package cn.life.common.support.jdbc.config;

import cn.life.common.support.datasource.config.DataSourceConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;


@Order(103)
@Configuration
@EnableTransactionManagement
public class JdbcConfiguration {

    @Autowired(required = true)
    @Qualifier("mySqlDataSource")
    private DataSource mysqlDataSource;

    @Autowired(required = true)
    @Qualifier("oracleDataSource")
    private DataSource oracleDataSource;

    @Primary
    @Bean(name = "mysqlJdbcTemplate")
    public JdbcTemplate mysqlJdbcTemplate() {
        return new JdbcTemplate(mysqlDataSource);
    }

    @Bean(name = "oracleJdbcTemplate")
    public JdbcTemplate oracleJdbcTemplate() {
        return new JdbcTemplate(oracleDataSource);
    }

    /**
     * 配置事物管理器
     *
     * @return
     */
    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(mysqlDataSource);
    }

    /**
     * 配置事物管理器
     *
     * @return
     */
    @Bean(name = "oracleTransactionManager")
    public PlatformTransactionManager oracleTransactionManager() {
        return new DataSourceTransactionManager(oracleDataSource);
    }

}
